package com.example.springbootdemo.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

@RestController
public class MailAction {

    @Resource
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(from);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            System.out.println("html格式邮件发送成功");
        } catch (Exception e) {
            System.out.println("html格式邮件发送失败");
        }
    }

    // 假定用户 yc 的邮箱地址是 306529917@qq.com

    @RequestMapping("forgetPwd")
    public String forgetPwd(String username, HttpSession session){
        String email = getEmailByUsername(username);
        String token = UUID.randomUUID().toString();
        session.setAttribute("forgetPwdToken", token);
        String content =
                "源辰公司XXX系统, 忘记密码请 <a href='http://127.0.0.1/toResetPwd?token="
                        +token+"'>" +
                "点击跳转到重置页面" +
                "</a>";
        sendHtmlMail(email,"忘记密码", content);
        return "邮件发送成功!";
    }

    @RequestMapping("toResetPwd")
    public ModelAndView toResetPwd( ModelAndView mav,
            String token, @SessionAttribute String forgetPwdToken)
            throws UnsupportedEncodingException {
        if(token.equals(forgetPwdToken) == false){
            final String addr = URLEncoder.encode("redirect:/忘记密码.html", "utf-8");
            mav.setViewName(addr);
            return mav;
        } else {
            mav.setViewName("resetPwd");
        }
        return mav;
    }

    private String getEmailByUsername(String username) {
        // 根据用户名从数据库查询 用户的邮箱地址 TODO
        if("yc".equals(username)){
            return "306529917@qq.com";
        } else {
            return null;
        }
    }


}
